<?php

class IndexController extends CustomControllerAction {

    public function init() {
        parent::init();
        // $cache определен в предыдущем примере
        // проверка, есть ли уже запись в кэше:
       if (!$cats = $this->cache->load('leftcat')) {
            // промах кэша
            $cats = DatabaseObject_Categories::GetAllParent($this->db);
            $this->cache->save($cats, 'leftcat');
        } else {
            // попадание в кэш
        }
      //  CustomControllerAction::pre($cats);
        $this->view->categories = $cats;
    }

    public function indexAction() {
        $this->view->page_title = $this->configuration['meta-title'];
        $this->view->page_keywords = $this->configuration['meta-keywords'];
        $this->view->page_description = $this->configuration['meta-descr'];
        $newproducts = DatabaseObject_Products::GetProducts($this->db, array('order' => 'id desc', 'limit' => '8'));
        $this->view->newproducts = $newproducts;
        $liderproducts = DatabaseObject_Products::GetTopProduct($this->db);

        $this->view->liderproducts = $liderproducts;
        $this->view->widget = '1';
    }

    public function categoriesAction() {
 
    }

    public function catalogAction() {

        if (isset($_GET['sort']) && isset($_GET['order'])) {
            $sort = $_GET['sort'];
            $order = $_GET['order'];
            $_SESSION['products_sort'] = $sort . ' ' . $order;
        } elseif (!empty($_SESSION['products_sort'])) {
            $temp = explode(' ', $_SESSION['products_sort']);
            $sort = $temp['0'];
            $order = $temp['1'];
        } else {
            $sort = 'date_add';
            $order = 'DESC';
            $_SESSION['products_sort'] = $sort . ' ' . $order;
        }
        $sortorder = strtolower($sort . $order);
        
        $page = max(1, $this->getRequest()->getQuery('p'));
        if ($this->getRequest()->getParam('cat')){
                    $cat = $this->getRequest()->getParam('cat');       
        }
        if ($this->getRequest()->getParam('subcat')){
                    $subcat = $this->getRequest()->getParam('subcat');       
        }else {$subcat ='';}
        if ($this->getRequest()->getParam('subsubcat')){
                    $subsubcat = $this->getRequest()->getParam('subsubcat');       
        }else {$subsubcat ='';}
        if ($this->getRequest()->getParam('subsubsubcat')){
                    $subsubsubcat = $this->getRequest()->getParam('subsubsubcat');       
        }else {$subsubsubcat ='';}
       
            
           // echo $subsubsubcat;echo $subsubcat;echo $subcat;echo $cat;
        $type = $this->getRequest()->getParam('type');
        if (isset($cat)) {
            $this->view->displayProd = true;
            $glav = DatabaseObject_Categories::GetAliasCat($this->db, $cat);
          
            $catId = DatabaseObject_Categories::GetAliasCat($this->db, $cat, $subcat, $subsubcat, $subsubsubcat);
            $catId = 26;
            $category = DatabaseObject_Categories::GetMetaTag($this->db,$catId);
            $categorlist = DatabaseObject_Categories::GetChild($this->db, $glav);
            $glavnaya = new DatabaseObject_Categories($this->db);
            $glavnaya->load($glav);
            $glavnaya->count = DatabaseObject_Products::GetUserProductCount($this->db, false, $glav);
            $this->view->glav = $glavnaya;
            $this->view->categories = $categorlist;
            //CustomControllerAction::pre($categorlist);
            if($category['title'])
            $this->view->page_title = $category['title'];
            if($category['descr'])
            $this->view->page_keywords = $category['descr'];
            if($category['keywords'])
            $this->view->page_description = $category['keywords'];
             $this->view->catalias = $cat;
        } else {
            $catId = 0;
        }
        $filter = array(
            'catId' => $catId,
            'type' => $type
        );
        
        $limit =$this->configuration['item_count'];
        $offset = ($page - 1) * $limit;
       
         $pages  = ceil(DatabaseObject_Products::GetProductsCount($this->db, array('order' => $_SESSION['products_sort']), $filter) / $limit);
         $products = DatabaseObject_Products::GetProducts($this->db, array('order' => $_SESSION['products_sort'], 'limit' => $limit, 'offset' => $offset), $filter);
        $this->view->products = $products;
        $this->view->p = $page;
        $pag = CustomControllerAction::Pag($pages,$page);
        $this->view->pages = $pag;

        $namecache = 'tags' . $catId . md5($catId . $type);
        if (!$tags = $this->cache->load($namecache)) {
            // промах кэша
            $tags = DatabaseObject_Products::GetProductsTags($this->db, array(), $filter);
            $this->cache->save($tags, $namecache);
        } else {
            // попадание в кэш
        }
       
        // CustomControllerAction::pre($k);
        // $this->view->products = $products;
        $this->view->p = $page;
        // $this->view->pages = $paginator->getPages()->pageCount;
        $this->view->widget = '2';
        if ($catId != 0) {
            $kroshki = DatabaseObject_Categories::GetParentKroshki($this->db, $catId);
            $kroshki = array_reverse($kroshki);
            $this->view->kroshki = $kroshki;
        }
  
        $this->view->cat = $catId;
       
        $this->view->type = $type;
        $this->view->tags = $tags;
        $this->view->sort = $sort;
        $this->view->order = $order;
        $this->view->db = $this->db;
    }

    public function searchAction() {
         $this->view->page_title ="Поиск";
        if (isset($_GET['sort']) && isset($_GET['order'])) {
            $sort = $_GET['sort'];
            $order = $_GET['order'];
            $_SESSION['products_sort'] = $sort . ' ' . $order;
        } elseif (!empty($_SESSION['products_sort'])) {
            $temp = explode(' ', $_SESSION['products_sort']);
            $sort = $temp['0'];
            $order = $temp['1'];
        } else {
            $sort = 'date_add';
            $order = 'DESC';
            $_SESSION['products_sort'] = $sort . ' ' . $order;
        }
             
       
  $page = max(1, $this->getRequest()->getQuery('p'));
   $limit =$this->configuration['item_count'];
        $offset = ($page - 1) * $limit;
        $sortorder = strtolower($sort . $order);
        $type = $this->getRequest()->getParam('type');
        $search_type = $this->getRequest()->getParam('search_type');
            $filter_name = $this->getRequest()->getParam('filter_name');
            $filter_material = $this->getRequest()->getParam('material');
            if (is_array($filter_material))
                $filter_material = implode(',', $filter_material);
            $filter_tags = $this->getRequest()->getParam('tags');
            if (is_array($filter_tags))
                $filter_tags = implode(',', $filter_tags);
            $max = 10; // максимальное количество слов во фразе
            $min_length = 3; // минимальная длина искомого слова
            if ($filter_name) {
                $word = explode(" ", CustomControllerAction::cleanPostData($filter_name));
                $words = CustomControllerAction::cleanArrayToSearch($word, $max, $min_length);
            } else $words ='';
            if ($filter_material) {
                $word = explode(",", CustomControllerAction::cleanPostData($filter_material));
                $material = CustomControllerAction::cleanArrayToSearch($word, $max, $min_length);
            } else $material =0;
            if ($filter_tags) {
                $word = explode(",", CustomControllerAction::cleanPostData($filter_tags));
                $tags = CustomControllerAction::cleanArrayToSearch($word, $max, $min_length);
            } else $tags =0;
        if ($search_type != 1) {
            $user= '';
           if ($search_type == 2) 
           {
            $filter = array('nameshop' => $filter_name);
           $user = DatabaseObject_User::GetSearchUsers($this->db, array(), $filter);}
            else{
                $id = $this->getRequest()->getParam('city_id_input');
                if ($id=='')
                {
                    $city = CustomControllerAction::getCityName($this->getRequest()->getParam('filter_city'));
                    if ($city)
                    $id = $city; 
                   
                 
                }
                
            $filter = array('city_id' => $id);
            $filter_name = $this->getRequest()->getParam('filter_city');
            if ($id)
            $user = DatabaseObject_User::GetSearchUsers($this->db, array(), $filter);
            }
            
            if ($user){
            $paginator = Zend_Paginator::factory($user);
            $paginator->setCurrentPageNumber($page);
            $paginator->setItemCountPerPage($this->configuration['item_count']);
            $this->view->user = $paginator; 
            $this->view->pages = $paginator->getPages();
            
            }
        }
        if ($search_type == 1) {
            $products = '';
            $filter = array(
                'search' => $words,
                'material' => $material,
                'tags' => $tags,
                'type' => $type
            );
           // $products = DatabaseObject_Products::GetSearchProducts($this->db, array(), $filter);
            $pages  = ceil(DatabaseObject_Products::GetSearchProductsCount($this->db, array('order' => $_SESSION['products_sort']), $filter) / $limit);
            echo $offset;
        $products = DatabaseObject_Products::GetSearchProducts($this->db, array('order' => $_SESSION['products_sort'], 'limit' => $limit, 'offset' => $offset), $filter);
        $this->view->products = $products;
        $this->view->p = $page;
        $pag = CustomControllerAction::Pag($pages,$page);
        $this->view->pages = $pag;
            //сортировка
           // uasort($products, $sortorder);
            $namecache = 'tags' . 0 . md5(0 . $type);
            if (!$tags = $this->cache->load($namecache)) {
                // промах кэша
                $tags = DatabaseObject_Products::GetProductsTags($this->db, array(), $filter);
                $this->cache->save($tags, $namecache);
            } else {
                // попадание в кэш
            }
            if ($products){
          //  $paginator = Zend_Paginator::factory($products);
          //  $paginator->setCurrentPageNumber($page);
           // $paginator->setItemCountPerPage($this->configuration['item_count']);
            $this->view->tags = $tags;
          //  $this->view->products = $paginator; 
           // $this->view->pages = $paginator->getPages();
            }
        }
        $this->view->filter_name = $filter_name;
        $this->view->filter_material = $filter_material;
        $this->view->search_type = $search_type;
        $this->view->filter_tags = $filter_tags;
        
        $this->view->type = $type;
        $this->view->widget = '2';
    }

}

?>
